package com.adminx.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.adminx.entity.Role;
import com.adminx.pojo.RolePojo;
import com.adminx.util.PageResults;

@Repository
public class RoleDao extends BaseDao<Role, Long>{
	
	/**
	 * 查询角色
	 * @param pageIndex
	 * @param pageSize
	 * @return PageResults<Role>
	 */
	public PageResults<RolePojo> findAllRole(Integer pageIndex, Integer pageSize, String searchText){

		String sql = 
			"SELECT " +
			"	t.role_id id, " +
			"	DATE_FORMAT( " +
			"		t.create_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) createDate, " +
			"	t.remark remark, " +
			"	DATE_FORMAT( " +
			"		t.update_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) updateDate, " +
			"	t.role_code roleCode, " +
			"	t.role_name roleName " +
			"FROM " +
			"	xx_role t " ;
			
		String countSql = 
			"SELECT " +
			"	count(1) " +
			"FROM " +
			"	xx_role t " ;
		
		if(!StringUtils.isBlank(searchText)){
			sql += "WHERE t.role_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
			countSql += "WHERE t.role_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
		}	
		
		return this.findPageByFetchedSql(RolePojo.class, sql, countSql, pageIndex, pageSize);
	}
	
	/**
	 * 通过角色ID获取角色
	 * @param roleID 角色ID
	 * @return 角色
	 */
	public RolePojo info(Long roleID){
		
		String sql = 
			"SELECT " +
			"	t.role_id id, " +
			"	DATE_FORMAT( " +
			"		t.create_time, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) createDate, " +
			"	t.remark remark, " +
			"	DATE_FORMAT( " +
			"		t.update_time, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) updateDate, " +
			"	t.role_code roleCode, " +
			"	t.role_name roleName " +
			"FROM " +
			"	xx_role t " +
			"WHERE " +
			"	t.role_id = " + roleID;
		
		return (RolePojo) this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(RolePojo.class)).uniqueResult();
	}
	
	/**
	 * 批量删除
	 * @param roleID 角色ID
	 * @return 角色
	 */
	public int delBatch(String ids){
		String[] idsArray = ids.split(",");
		String hql = "";
		for (int i = 0; i < idsArray.length; i++) {
			if (i == 0) {
				hql = "roleId=" + idsArray[i];
			} else {
				hql = hql + " or roleId=" + idsArray[i];
			}
		}
		int updateCount = this.getSession().createQuery("DELETE FROM Role WHERE " + hql).executeUpdate();
		return updateCount;
	}
	
	/**
	 * 取消角色权限
	 */
	public void removeAuthority(Long roleID){
		String sql = "delete t from  xx_role_authority t where  t.role_id = "+roleID;
		this.getSession().createSQLQuery(sql).executeUpdate();
	}
	
	/**
	 * 给角色赋权限
	 */
	public void grantAuthority(Long roleID,String authoritys){
		String[] auths = authoritys.split(",");
		String sql = "";
		for(String authorityID: auths){
			sql = "INSERT INTO xx_role_authority (role_id, authority_id) VALUES ("+roleID+", "+authorityID+")";
			this.getSession().createSQLQuery(sql).executeUpdate();
		}
	}
	
	/**
	 * 查询当前账号角色拥有
	 * @param accountId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<RolePojo> findRoleListByAccount(Long accountId){
	
		String sql = 
			"SELECT " +
			"	t.role_id id, " +
			"	t.role_name roleName, " +
			"	t.role_code roleCode, " +
			"	DATE_FORMAT( " +
			"		t.create_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) createDate, " +
			"	t.remark, " +
			"	DATE_FORMAT( " +
			"		t.update_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) updateDate, " +
			"	IFNULL(a.role_id, 0) hasRole " +
			"FROM " +
			"	xx_role t " +
			"LEFT JOIN xx_account a ON t.role_id = a.role_id " +
			"AND a.account_id = " + accountId;
		return this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(RolePojo.class)).list();
	}
}
